package cn.maihe.elg.operation.web.system;


import cn.maihe.elg.operation.common.Constants;
import cn.maihe.elg.operation.exception.ErrorCode;
import cn.maihe.elg.operation.exception.SystemRuntimeException;
import cn.maihe.elg.operation.model.vo.system.CaptchaRespDTO;
import cn.maihe.elg.operation.model.vo.system.CaptchaVO;
import cn.maihe.elg.operation.utils.IdGenerate;
import cn.maihe.elg.operation.utils.SystemClock;
import com.google.code.kaptcha.Producer;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @Description
 * @Author caixh
 * @Date 2021/6/7.
 */
@Controller
@Slf4j
public class CaptchaController {

    @Qualifier("captchaProducer")
    @Autowired
    private Producer captchaProducer;

    @GetMapping("/api/auth/authCode")
    public void authCode(HttpServletRequest request, HttpServletResponse response){
        String capStr, code = null;
        BufferedImage image = null;
        capStr = code = captchaProducer.createText();
        log.debug("验证码：{}",capStr);
        image = captchaProducer.createImage(capStr);
        request.getSession().setAttribute(Constants.CAPTCHA_KEY,capStr);

        try {
            response.setContentType("image/png");
            OutputStream os = response.getOutputStream();
            ImageIO.write(image,"png",os);
        } catch (IOException e) {
            log.error("验证码失败",e);
        }
    }

    @RequestMapping("/api/auth/authCode64")
    @ResponseBody
    public CaptchaRespDTO getAuthCode(){
        if(Constants.CAPTCHA_HASHMAP.size()>1000){
            throw new SystemRuntimeException(ErrorCode.CAPT001, "");
        }

        String capStr = null;
        BufferedImage image = null;
        capStr = captchaProducer.createText();
        log.debug("验证码：{}",capStr);
        image = captchaProducer.createImage(capStr);

        String uuid = IdGenerate.get32UUID();

        // 转换流信息写出
        FastByteArrayOutputStream os = new FastByteArrayOutputStream();
        try{
            ImageIO.write(image, "jpg", os);
        }
        catch (IOException e) {
            throw new SystemRuntimeException(ErrorCode.CAPT002, "");
        }
        CaptchaRespDTO captchaRespDTO = new CaptchaRespDTO();
        captchaRespDTO.setImageBase64("data:image/jpeg;base64," + Base64.encode(os.toByteArray()));
        captchaRespDTO.setToken(uuid);

        CaptchaVO vo = new CaptchaVO();
        vo.setCaptcha(capStr);
        vo.setTtlTime(SystemClock.now()+300000);
        Constants.CAPTCHA_HASHMAP.put(uuid,vo);

        return captchaRespDTO;
    }

}
